package com.sgcc.epri.dcloud.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sgcc.epri.dcloud.mapper.dm.FractureSurfaceOrcMapper;
import com.sgcc.epri.dcloud.pojo.FractureSurfaceOrc;
import com.sgcc.epri.dcloud.pojo.Result;
import com.sgcc.epri.dcloud.pojo.SectionOrc;
import com.sgcc.epri.dcloud.service.FractureSurfaceOrcService;
import com.sgcc.epri.dcloud.util.ChoreUtil;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class FractureSurfaceOrcServiceImpl implements FractureSurfaceOrcService {

    private final Logger log =  LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FractureSurfaceOrcMapper fractureSurfaceOrcMapper;

    public Result selectData(Integer page, Integer limit, String key) {
        try{
            Page<FractureSurfaceOrc> pl = new Page<>(page, limit);
            QueryWrapper<FractureSurfaceOrc> oqw = new QueryWrapper<>();
            if (!ChoreUtil.isAlpha(key)) {
                oqw.like("dev_name",key).or().
                like("dev_id",key).or().
                like("asset_id",key).or().
                like("asset_name",key);
            }
            return Result.success(fractureSurfaceOrcMapper.selectPage(pl, oqw));
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询失败！");
            return Result.error("查询失败！");
        }
    }

    @Override
    public Result updateData(FractureSurfaceOrc fractureSurfaceOrc) {
        try {
            return Result.success(fractureSurfaceOrcMapper.updateById(fractureSurfaceOrc));
        }catch (Exception e){
            e.printStackTrace();
            log.error("修改失败！");
            return Result.error("修改失败！");
        }
    }

    @Autowired
    @Qualifier("dmSqlSessionFactory")
    private SqlSessionFactory sqlSessionFactory;

    @Override
    public Result insertData(SectionOrc sectionOrc) {
        try{

            List<String> assetIds = null;
            List<String> devIds = null;

            try {
                assetIds = Arrays.asList(sectionOrc.getAssetId().split(","));
                devIds = Arrays.asList(sectionOrc.getDevId().split(","));
            } catch (Exception e){
                e.printStackTrace();
                log.error("以逗号切割断面映射关系过程中失败！");
                return Result.success("解析失败！，请检查您的格式...");
            }

            Map<String, Object> assetNameOrc = ChoreUtil.getTableNameMap(fractureSurfaceOrcMapper.queryAssetNameBy0401());
            Map<String, Object> devNameOrc = ChoreUtil.getTableNameMap(fractureSurfaceOrcMapper.queryDevName0401());

            // 关闭session的自动提交
            SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
            FractureSurfaceOrcMapper fractureSurfaceOrcMapper = sqlSession.getMapper(FractureSurfaceOrcMapper.class);

            try{

                for (String aid : assetIds) {
                    for (String did : devIds) {
                        FractureSurfaceOrc f = new FractureSurfaceOrc();
                        f.setAssetId(aid.trim());
                        f.setDevId(did.trim());
                        f.setAssetName(assetNameOrc.get(aid)+"");
                        f.setDevName(devNameOrc.get(did)+"");
                        fractureSurfaceOrcMapper.insert(f);
                    }
                }

                sqlSession.commit();
            }catch (Exception e){
                e.printStackTrace();
                log.info("断面关系入库时出错");
                return Result.error("断面信息入库时出错！");
            }finally {
                sqlSession.close();
            }

            return Result.success("操作完成");
        }catch (Exception e){
            e.printStackTrace();
            log.error("插入失败！");
            return Result.error("插入失败！");
        }
    }

    @Override
    public Result deleteData(String id) {
        try{
            return Result.success(fractureSurfaceOrcMapper.deleteById(id));
        }catch (Exception e){
            e.printStackTrace();
            log.error("删除失败！");
            return Result.error("删除失败！");
        }
    }

}
